<?php
session_start();

if (!isset($_SESSION['AccUnique']) || $_SESSION['AccUnique'] == "") {
    echo "<script type='text/javascript'>location.href='index.php'</script>";
}

require_once 'common/config.php';
require_once 'common/conn.php';
require_once 'common/function.php';

require_once 'db/item.db.php';
require_once 'db/itemType.db.php';
require_once 'db/accountMoney.db.php';

$itemNoArray = array();

while ($itemNo = current($_POST)) {
    array_push($itemNoArray, key($_POST));
    next($_POST);
}

$dbc = new DBC();

$itemNos = implode(",", $itemNoArray);

$itemDb = new ItemDb($dbc);
$itemCartArray = $itemDb->getItemByNo($itemNos);

$totalAmount = 0;
for ($i = 0; $i < count($itemCartArray); $i++) {
    $itemCartArray[$i]['num'] = $_POST[$itemCartArray[$i]['ItemNo']];
    $itemCartArray[$i]['amount'] = $itemCartArray[$i]['Price'] * $itemCartArray[$i]['num'];
    $totalAmount += intval($itemCartArray[$i]['amount']);
}

$_SESSION['cartReady'] = $itemCartArray;
$_SESSION['totalAmount'] = $totalAmount;
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>荣耀ol - 在线商城</title>
        <meta name="description" content="Flat UI Kit Free is a Twitter Bootstrap Framework design and Theme, this responsive framework includes a PSD and HTML version."/>

        <meta name="viewport" content="width=1000, initial-scale=1.0, maximum-scale=1.0">

        <!-- Loading Bootstrap -->
        <link href="<?php echo $flatUi; ?>dist/css/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

        <!-- Loading Flat UI -->
        <link href="<?php echo $flatUi; ?>dist/css/flat-ui.css" rel="stylesheet">
        <link href="<?php echo $flatUi; ?>docs/assets/css/demo.css" rel="stylesheet">

        <link rel="shortcut icon" href="<?php echo $flatUi; ?>img/favicon.ico">

        <script src="<?php echo $flatUi; ?>js/jquery-1.11.3.js"></script>
        <script type="text/javascript">

            function addCart(itemNo) {
                $.ajax({
                    type: 'post',
                    url: 'api/cart.php',
                    data: {action: 'addCart', item_code: itemNo},
                    dataType: 'json',
                    success: function(data) {
                        console.log(data);
                        alert("已加入购物车");
                    },
                    error: function(e, f, g) {
                        console.log(e);
                    }
                });
            }

        </script>
        <style>
            html body{
                width:800px;
                height:586px;
            }
            td{
                text-align: center;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <!--            <h1 class="demo-section-title text-center">荣耀ol - 在线商城</h1>-->
            <h3 class="demo-panel-title"><a href="index.php">回首页</a></h3>
            <div class="row">
                <div class="col-xs-12" style="height:50px;">
                    <?php
                    if (isset($_SESSION['AccountID']) && $_SESSION['AccountID'] != "") {
                        $accountMoneyDb = new AccountMoneyDb($dbc);
                        $accountMoney = $accountMoneyDb->getAccountMoney($_SESSION['AccUnique']);
                        $balance = 0;
                        if ($accountMoney && isArrayNotEmpty($accountMoney)) {
                            $balance = $accountMoney[0]['Balance'];
                        }
                        ?>
                        用户：<?php echo $_SESSION['AccountID']; ?>&nbsp;&nbsp;余额：<?php echo $balance; ?>&nbsp;&nbsp;<a href="cart.php">查看购物车</a>
                    <?php } else { ?>
                        <form action="action/account.action.php" method="post">
                            <div class="demo-browser-side">
                                <div class="demo-browser-author"></div>
                                <div class="demo-browser-action">
                                    <input type="hidden" name="action" value="login"/>
                                    <input type="text" class="form-control" name="username" placeholder="用户名"/>
                                    <br>
                                    <input type="password" class="form-control" name="password" placeholder="密码"/>
                                    <br>
                                    <input type="submit" class="form-control" value="登录"/>
                                </div>
                            </div>
                        </form>
                    <?php } ?>
                </div>
                <form action="action/cart.action.php" method="post">
                    <div class="col-xs-12">
                        <div class="row demo-tiles">
                            <table class="table-bordered" style="width:780px;">
                                <tr><td style="width: 10%;">序号</td><td style="width: 40%;">名称</td><td style="width: 20%;">图片</td><td style="width: 20%;">数量</td><td>价格</td></tr>
                                <?php
                                for ($i = 0; $i < count($itemCartArray); $i++) {
                                    $itemNo = $itemCartArray[$i]['ItemNo'];
                                    $itemName = $itemCartArray[$i]['ItemName'];
                                    $imageUrl = $itemCartArray[$i]['ImageUrl'];
                                    $num = $itemCartArray[$i]['num'];
                                    ?>
                                    <tr><td><?php echo $i + 1; ?></td>
                                        <td><?php echo $itemName; ?></td>
                                        <td><img src="<?php echo $imageUrl; ?>" style="width:80px;height:80px;"></td>
                                        <td><?php echo $num; ?></td>
                                        <td><?php echo $itemCartArray[$i]['amount']; ?>
                                            <input name="<?php echo $itemNo; ?>" type="hidden" value="<?php echo $itemCartArray[$i]['amount']; ?>">
                                        </td>
                                    </tr>
                                    <?php
                                }
                                ?>
                                <tr><td>合计</td><td></td><td></td><td></td><td><?php echo $totalAmount; ?></td></tr>
                            </table>
                        </div> <!-- /tiles -->
                        <div style="text-align: center">
                            <?php if ($balance > $totalAmount) { ?>
                                <input type="submit" value="确认购买">
                            <?php } else { ?>
                                <input type="button" value="余额不足，无法购买" class="disabled">
                            <?php } ?>
                        </div>
                    </div>
                </form>
            </div> <!-- /download area -->
        </div> <!-- /container -->
    </body>
</html>
